package com.scuec.groupfive.mapper;

import com.scuec.groupfive.vo.DailyLearningTrendVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface DailyLearningTrendMapper {

    /**
     * 查询用户近三日答题趋势
     * @param userId 用户ID
     * @return 答题趋势列表
     */
    @Select("WITH RECURSIVE date_range AS (" +
            "    SELECT CURRENT_DATE AS record_date" +
            "    UNION ALL" +
            "    SELECT record_date - INTERVAL 1 DAY" +
            "    FROM date_range" +
            "    WHERE record_date > CURRENT_DATE - INTERVAL 2 DAY" +
            ") " +
            "SELECT " +
            "    dr.record_date AS date, " +
            "    COALESCE(COUNT(ar.record_id), 0) AS answerCount " +
            "FROM date_range dr " +
            "LEFT JOIN answer_records ar " +
            "    ON dr.record_date = DATE(ar.answered_at) " +
            "    AND ar.user_id = #{userId} " +
            "GROUP BY dr.record_date " +
            "ORDER BY dr.record_date DESC")
    List<DailyLearningTrendVO> selectDailyAnswerTrend(@Param("userId") Integer userId);
}